package com.steel.yfmall.cloud.assistant.config;

import com.alibaba.fastjson.JSON;
import com.steel.yfmall.cloud.assistant.common.CacheRedisService;
import com.steel.yfmall.cloud.assistant.po.view.OutCallApiLogBean;
import com.steel.yfmall.cloud.components.mq.bean.RequestSendMQBean;
import com.steel.yfmall.cloud.components.mq.service.MqSendService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class WeblogIdInterceptor implements HandlerInterceptor {

    /**
     * 拦截方法
     * @Title: preHandle
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param request 参数说明
     * @Param response 参数说明
     * @Param handler 参数说明
     * @return boolean    返回类型
     * @author yuchaohua
     * @date 2021/1/22 18:52
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        BodyReaderHttpServletRequestWrapper bodyReaderHttpServletRequestWrapper = new BodyReaderHttpServletRequestWrapper(request);
        String body = bodyReaderHttpServletRequestWrapper.getBody();
        String reqUrl = request.getRequestURL().toString();
        if (outCallAssistantProperties != null && outCallAssistantProperties.getAppLogApiMap() != null && outCallAssistantProperties.getAppLogApiMap().size() > 0){
            Map<String, String> appLogApiMap = outCallAssistantProperties.getAppLogApiMap();
            // 判断是否需要记录日志
            for (Map.Entry<String, String> ass : appLogApiMap.entrySet()) {
                if (StringUtils.indexOf(reqUrl, ass.getKey()) >= 0) {
                    // 记录日志
                    addCommonCallApiLog(replaceBlank(body), null, reqUrl, ass.getKey(), ass.getValue());
                }
            }
        }
        return true;
    }

    /**
     * 记录接口访问日志
     * @Title: addCommonCallApiLog
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param paraStr 参数说明
     * @Param resultText 参数说明
     * @Param interfaceMethod 参数说明
     * @Param interfaceMethodName 参数说明
     * @return void    返回类型
     * @author yuchaohua
     * @date 2021/1/22 18:04
     */
    @SuppressWarnings("rawtypes")
    protected void addCommonCallApiLog(String paraStr, String resultText, String apiUrl, String interfaceMethod, String interfaceMethodName) {
        OutCallApiLogBean outCallApiLogBean = new OutCallApiLogBean();
        outCallApiLogBean.setParaText(paraStr);
        outCallApiLogBean.setInterfaceMethod(interfaceMethod);
        outCallApiLogBean.setInterfaceMethodName(interfaceMethodName);
        outCallApiLogBean.setApiUrl(apiUrl);
        outCallApiLogBean.setResultText(resultText);
        outCallApiLogBean.setStatus(1);
        //下发mq，调整订单及交货单状态
        RequestSendMQBean requestSendMQBean = new RequestSendMQBean();
        requestSendMQBean.setSendMqTitle("insert_out_call_api_log");
        requestSendMQBean.setSendMqMessage(JSON.toJSONString(outCallApiLogBean));
        mqSendService.sendCommonMQMessage(requestSendMQBean);
    }

    public static String replaceBlank(String str) {
        String dest = "";
        if (StringUtils.isNotBlank(str)) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }

    /**
     * 校验配置
     */
    @Resource
    private OutCallAssistantProperties outCallAssistantProperties;

    @Resource
    protected MqSendService mqSendService;

    @Resource
    private CacheRedisService cacheRedisService;
}
